package com.example.demo.handle;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器数据预处理
 * @author LF
 * @date 2021/4/19 - 14:48
 */
@Component
@Slf4j
public class MyInterceptor implements HandlerInterceptor {
    /**
     * 预处理回调方法
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws  Exception{
        log.info("拦截器开始拦截....URL------->    {}", request.getRequestURI());
        return true;
    }

    /**
     * 处理逻辑，在controller后调用
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        log.info("拦截器工作中....URL------->    {}", request.getRequestURI());
    }

    /**
     * 处理完成后处理逻辑
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        log.info("拦截器已经结束....URL------->    {}", request.getRequestURI());
    }

}
